//
//  addWishViewController.m
//  TinyFortune
//
//  Created by lc on 16/6/27.
//  Copyright © 2016年 lc. All rights reserved.
//

#import "addWishViewController.h"

@interface addWishViewController ()<UITextViewDelegate>

@end

@implementation addWishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)createUI{
    //实例化导航条
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    [self.navigationItem setLeftBarButtonItem:leftBar];
    self.navigationController.navigationBar.translucent = NO ;
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    UIBarButtonItem *rightEditBar = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)];
    [self.navigationItem setRightBarButtonItem:rightEditBar];
    [self setTitle:@"礼盒"];
    [self.view setBackgroundColor:[UIColor colorWithRed:239/255.0f green:239/255.0f blue:244/255.0f alpha:1]];
    
    self.wishImage.image = self.image;
    [self.wishTitle becomeFirstResponder];
    self.wishDes.layer.masksToBounds = YES ;
    self.wishDes.layer.cornerRadius = 8 ;
    self.wishTitle.layer.masksToBounds = YES ;
    self.wishTitle.layer.cornerRadius = 5;
    
    self.wishDes.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    self.wishTitle.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
    self.wishTitle.placeholder = @"题目" ;
    self.wishDes.delegate =  self ;
    self.wishDes.text = @"描述" ;
    
    
}
#pragma mark - 文本协议
- (void)textViewDidBeginEditing:(UITextView *)textView{
    textView.text = @"" ;
}
#pragma mark - 按钮实现
- (void)edit:(UIBarButtonItem *)sender{
    AVObject *obj = [[AVObject alloc]initWithClassName:@"whish"];
    [obj setObject:self.wishTitle.text forKey:@"title"];
    [obj setObject:self.wishDes.text forKey:@"desc"];
    NSData *data = UIImageJPEGRepresentation(self.image, 0.01);
    AVFile *file = [AVFile fileWithData:data];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [obj setObject:[AVUser currentUser].mobilePhoneNumber forKey:@"user"];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            MBProgressHUD *hud = [MBProgressHUD HUDForView:self.view];
            [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [hud hide:YES];
                    [self cancel];
                }
                
            }];

            [obj setObject:file.objectId forKey:@"wishId"];
        }else{
             NSLog(@"%@",error.localizedDescription);
        }
    }];
    
}
- (void)cancel{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
